package org.edu.page;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.lang3.ArrayUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class MapUtils
{
    public static String getString(String key, Map<String, Object> map)
    {
        if ((map == null) || (key == null)) {
            throw new IllegalArgumentException();
        }
        if (!map.containsKey(key)) {
            return null;
        }
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        return value.toString();
    }

    public static Integer getInteger(String key, Map<String, Object> map)
    {
        if ((map == null) || (key == null)) {
            throw new IllegalArgumentException();
        }
        if (!map.containsKey(key)) {
            return null;
        }
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if ((value instanceof Integer)) {
            return (Integer)value;
        }
        if ((value instanceof String)) {
            return Integer.valueOf((String)value);
        }
        if ((value instanceof java.sql.Date)) {
            throw new ClassCastException();
        }
        if ((value instanceof Number)) {
            return Integer.valueOf(((Number)value).intValue());
        }
        throw new ClassCastException();
    }

    public static Long getLong(String key, Map<String, Object> map)
    {
        if ((map == null) || (key == null)) {
            throw new IllegalArgumentException();
        }
        if (!map.containsKey(key)) {
            return null;
        }
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if ((value instanceof Long)) {
            return (Long)value;
        }
        if ((value instanceof Number)) {
            return Long.valueOf(((Number)value).longValue());
        }
        if ((value instanceof String)) {
            return Long.valueOf((String)value);
        }
        if ((value instanceof java.sql.Date)) {
            return Long.valueOf(((java.sql.Date)value).getTime());
        }
        if ((value instanceof Time)) {
            return Long.valueOf(((Time)value).getTime());
        }
        if ((value instanceof Timestamp)) {
            return Long.valueOf(((Timestamp)value).getTime());
        }
        throw new ClassCastException();
    }

    public static Double getDouble(String key, Map<String, Object> map)
    {
        if ((map == null) || (key == null)) {
            throw new IllegalArgumentException();
        }
        if (!map.containsKey(key)) {
            return null;
        }
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if ((value instanceof Double)) {
            return (Double)value;
        }
        if ((value instanceof Number)) {
            return Double.valueOf(((Number)value).doubleValue());
        }
        if ((value instanceof String)) {
            return Double.valueOf((String)value);
        }
        throw new ClassCastException();
    }

    public static BigDecimal getBigDecimal(String key, Map<String, Object> map)
    {
        if ((map == null) || (key == null)) {
            throw new IllegalArgumentException();
        }
        if (!map.containsKey(key)) {
            return null;
        }
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if ((value instanceof BigDecimal)) {
            return (BigDecimal)value;
        }
        if ((value instanceof Integer)) {
            return new BigDecimal(((Integer)value).intValue());
        }
        if ((value instanceof Short)) {
            return new BigDecimal(((Short)value).shortValue());
        }
        if ((value instanceof Byte)) {
            return new BigDecimal(((Byte)value).byteValue());
        }
        if ((value instanceof Long)) {
            return new BigDecimal(((Long)value).longValue());
        }
        if ((value instanceof Float)) {
            return new BigDecimal(((Float)value).floatValue());
        }
        if ((value instanceof Double)) {
            return new BigDecimal(((Double)value).doubleValue());
        }
        if ((value instanceof java.sql.Date)) {
            return new BigDecimal(((java.sql.Date)value).getTime());
        }
        if ((value instanceof Time)) {
            return new BigDecimal(((Time)value).getTime());
        }
        if ((value instanceof Timestamp)) {
            return new BigDecimal(((Timestamp)value).getTime());
        }
        if ((value instanceof String))
        {
            if (!StringUtils.isNullOrEmpty((String)value)) {
                return new BigDecimal((String)value);
            }
            return null;
        }
        throw new ClassCastException();
    }

    public static Map<String, Object> getMap(Object bean)
    {
        return beanToMap(bean);
    }

    public static void toLikeValue(Map<String, Object> map, String... likeKey)
    {
        if (ArrayUtils.isEmpty(likeKey)) {
            return;
        }
        for (String key : likeKey) {
            if (map.containsKey(key)) {
                map.put(key, "%" + map.get(key) + "%");
            }
        }
    }

    public static java.sql.Date getDate(String key, Map<String, Object> map)
    {
        if ((map == null) || (key == null)) {
            throw new IllegalArgumentException();
        }
        if (!map.containsKey(key)) {
            return null;
        }
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if ((value instanceof java.sql.Date)) {
            return (java.sql.Date)value;
        }
        if ((value instanceof Timestamp)) {
            return new java.sql.Date(((Timestamp)value).getTime());
        }
        return null;
    }

    public static java.util.Date getTimestamp(String key, Map<String, Object> map)
    {
        if ((map == null) || (key == null)) {
            throw new IllegalArgumentException();
        }
        if (!map.containsKey(key)) {
            return null;
        }
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if ((value instanceof java.sql.Date)) {
            return (java.sql.Date)value;
        }
        if ((value instanceof Timestamp))
        {
            Timestamp ts = (Timestamp)value;
            return ts;
        }
        return null;
    }

    public static Map<String, Object> beanToMap(Object beanObj)
    {
        Map<String, Object> params = new HashMap(0);
        try
        {
            PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
            PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(beanObj);
            for (int i = 0; i < descriptors.length; i++)
            {
                String name = descriptors[i].getName();
                if (!"class".equals(name)) {
                    params.put(name, propertyUtilsBean.getNestedProperty(beanObj, name));
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return params;
    }

    public static String convertMap2Xml(Map<Object, Object> paraMap)
    {
        StringBuffer xmlStr = new StringBuffer();
        if (paraMap != null)
        {
            xmlStr.append("<xml>");
            Set<Object> keySet = paraMap.keySet();
            Iterator<Object> keyIte = keySet.iterator();
            while (keyIte.hasNext())
            {
                String key = (String)keyIte.next();
                String val = String.valueOf(paraMap.get(key));
                xmlStr.append("<");
                xmlStr.append(key);
                xmlStr.append(">");
                xmlStr.append(val);
                xmlStr.append("</");
                xmlStr.append(key);
                xmlStr.append(">");
            }
            xmlStr.append("</xml>");
        }
        return xmlStr.toString();
    }

    public static Map<String, String> xmlToMap(HttpServletRequest request)
            throws IOException, DocumentException
    {
        Map<String, String> map = new HashMap();
        SAXReader reader = new SAXReader();
        InputStream ins = request.getInputStream();Throwable localThrowable3 = null;
        try
        {
            Document doc = reader.read(ins);
            Element root = doc.getRootElement();
            List<Element> list = root.elements();
            for (Object localObject1 = list.iterator(); ((Iterator)localObject1).hasNext();)
            {
                Element e = (Element)((Iterator)localObject1).next();
                map.put(e.getName(), e.getText());
            }
            return map;
        }
        catch (Throwable localThrowable1)
        {
            localThrowable3 = localThrowable1;throw localThrowable1;
        }
        finally
        {
            if (ins != null) {
                if (localThrowable3 != null) {
                    try
                    {
                        ins.close();
                    }
                    catch (Throwable localThrowable2)
                    {
                        localThrowable3.addSuppressed(localThrowable2);
                    }
                } else {
                    ins.close();
                }
            }
        }
    }

    public static Map<String, String> doXMLParse(String xml)
            throws DocumentException
    {
        Map<String, String> map = new HashMap();
        Document document = DocumentHelper.parseText(xml);
        Element root = document.getRootElement();
        Iterator<?> it = root.elementIterator();
        while (it.hasNext())
        {
            Element ele = (Element)it.next();
            map.put(ele.getName(), ele.getTextTrim());
        }
        return map;
    }
}
